﻿// 
// $Id: ScriptTemplates.cs 3982 2010-11-02 11:13:56Z unknown $
// 
// Zen UI Toolkit Copyright (c) 2010, Paul Clancy.  All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are permitted provided 
// that the following conditions are met:
// 
// * 	Redistributions of source code must retain the above copyright notice, this list of 
// 	conditions and the following disclaimer.
// 
// * 	Redistributions in binary form must reproduce the above copyright notice, this list of 
// 	conditions and the following disclaimer in the documentation and/or other materials 
// 	provided with the distribution.
// 
// * 	Neither the name of AppTek nor the names of its contributors may be used to endorse 
// 	or promote products derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
// THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 

#region

using System;

#endregion

namespace Zen.Toolkit
{
    [Flags]
    public enum ScriptTemplateFlags
    {
        None,
        Inline,
        Block
    }

    public class ScriptTemplate : IScriptTemplate
    {
        public const string DojoThemeStyleLink =
            "http://ajax.googleapis.com/ajax/libs/dojo/1.5/dijit/themes/{0:l}/{0:l}.css";

        public const string DojoLibraryUrl = "http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js";
        //public const string DojoLibraryUrl = "http://o.aolcdn.com/dojo/1.5/dojo/dojo.xd.js";


        public static ScriptTemplate NullFragment = new ScriptTemplate("{0}\n");

        public static ScriptTemplate InlineScript =
            new ScriptTemplate("\n\n<script type=\"text/javascript\">{0}</script>\n", ScriptTemplateFlags.Block);

        public static ScriptTemplate IncludeScript =
            new ScriptTemplate("\n\n<script type=\"text/javascript\" src=\"{0}\"></script>\n", ScriptTemplateFlags.Block);

        //public static ScriptTemplate ImportStyle = new ScriptTemplate("<style type=\"text/css\">\n@import url(\"{0}\");\n</style>", ScriptTemplateFlags.Block);
        public static ScriptTemplate ImportStyle =
            new ScriptTemplate("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", ScriptTemplateFlags.Block);

        public static ScriptTemplate DojoConstructorWithNode = new ScriptTemplate("var {0} = new {1}({{{2}}},\"{3}\");");
        public static ScriptTemplate DojoConstructor = new ScriptTemplate("var {0} = new {1}({{{2}}});");
        public static ScriptTemplate DojoRequire = new ScriptTemplate("dojo.require(\"{0}\");");
        public static ScriptTemplate DojoOnLoad = new ScriptTemplate("dojo.addOnLoad(function() {{ {0} }});");
        //public static ScriptTemplate DojoThemeBodyClass = new ScriptTemplate("document.body.className = \"{0:l}\";");

        public static ScriptTemplate ZenCallback = new ScriptTemplate("function() {{ $zen.execute('{0}', '{1}'); }}");
        public static ScriptTemplate FuncWrapper = new ScriptTemplate("function() {{ {0}; }}");

        public ScriptTemplate(string templateFormat, ScriptTemplateFlags flags)
        {
            TemplateFormat = templateFormat;
            Flags = flags;
        }

        public ScriptTemplate(string templateFormat)
        {
            TemplateFormat = templateFormat;
            Flags = ScriptTemplateFlags.Inline;
        }

        #region IScriptTemplate Members

        public string TemplateFormat { get; set; }
        public ScriptTemplateFlags Flags { get; set; }

        public string Build(params object[] args)
        {
            return ScriptManager.Format(TemplateFormat, args);
        }

        #endregion
    }
}